// // 用于添加动态路由
// import { useStore } from '@/stores/counter'

// const store = useStore()
import { getlocalStorage, setlocalStorage } from "@/hooks/operationLocalStorage"


interface Path {
  path: string;
  meta?: any
  component?: any;
  children?: Path[];
  redirect?: string
}

const obj: any = {
  'news': () => import('@/views/home/news/index.vue'),
  'friend': () => import("@/views/home/friend/friend.vue"),
  'adms': () => import("@/views/home/admin/adms.vue"),
  'mynotice': () => import("@/views/home/mynotice/index.vue"),
  'organization': () => import("@/views/home/organization/index.vue"),
  'newsecharts': () => import('@/views/home/news/echarts/newEcharts.vue'),
  'shownews': () => import("@/views/home/news/news.vue"),
  "sendnews": () => import('@/views/home/news/setNews.vue')
}

//替换component为组件
function gsh(paths: Path[]): Path[] {
  paths.forEach(e => {
    e.component = obj[e.component]
    if (e.children)
      gsh(e.children)
  })
  return paths
}

let paths: Path[] = [
  {
    "path": "adms",
    meta: {
      "value": "用户管理",
      "index": "1",
    },
    "component": 'adms',
  },
  {
    "path": "organization",
    meta: {
      "value": "组织管理",
      "index": "2",
    },
    "component": 'organization'
  },
  {
    "path": "news",
    meta: {
      "value": "新闻专栏",
      "index": "3",
    },
    "component": 'news',
    "children": [
      {
        path: '',
        redirect: '/home/news/shownews'
      },
      {
        "path": "shownews",
        meta: {
          "value": "查看新闻",
          "index": "3-1",
        },
        "component": "shownews"
      },
      {
        "path": "sendnews",
        meta: {
          "value": "发布新闻",
          "index": "3-2",
        },
        "component": "sendnews"
      },
      {
        "path": "newsecharts",
        meta: {
          "value": "新闻数据",
          "index": "3-3",
        },
        "component": 'newsecharts'
      }
    ]
  },
  {
    "path": "friend",
    meta: {
      "value": "朋友聊天",
      "index": "4",
    },
    "component": 'friend'
  },
  {
    "path": "mynotice",
    meta: {
      "value": "我的通知",
      "index": "5",
    },

    "component": 'mynotice'
  }
]
let paths2: Path[] = [
  {
    "path": "news",
    meta: {
      "value": "新闻专栏",
      "index": "1",
    },
    "component": 'news',
    "children": [
      {
        path: '',
        redirect: '/home/news/shownews'
      },
      {
        "path": "shownews",
        meta: {
          "value": "查看新闻",
          "index": "1-1",
        },
        "component": "shownews"
      },
      {
        "path": "sendnews",
        meta: {
          "value": "发布新闻",
          "index": "1-2",
        },
        "component": "sendnews"
      },
      {
        "path": "newsecharts",
        meta: {
          "value": "新闻数据",
          "index": "1-3",
        },
        "component": 'newsecharts'
      }
    ]
  },
  {
    "path": "organization",
    meta: {
      "value": "组织管理",
      "index": "4",
    },
    "component": 'organization'
  },
  {
    "path": "friend",
    meta: {
      "value": "朋友聊天",
      "index": "2",
    },
    "component": 'friend'
  },
  {
    "path": "mynotice",
    meta: {
      "value": "我的通知",
      "index": "3",
    },

    "component": 'mynotice'
  }

]
/**
 * 动态添加路由
 */
export default function addRouter(router: any, isRouter: boolean, power: string | number = 1) {

  //如果是router那里的调用，那么就是刷新页面，就不需要存储
  if (!isRouter) {
    power <= 2 ? setlocalStorage('addRouter', paths) : setlocalStorage('addRouter', paths2)
  }
  let admRouters: Path[] = getlocalStorage("addRouter") || null
  //如果是null则需要登录
  if (admRouters) {
    gsh(admRouters).forEach((element: Path) => {


      //判断插入的路由有无对应的组件
      if (element.component) {
        router.addRoute('home', element)
        //判断是否有子路径

      }


    });
  }
  // console.log(router.getRoutes());
  // console.log(router.options);
  return admRouters
}
